<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>还款记录</title>
    <style type="text/css">
    </style>
    <jsp:include page="../../import.jsp" flush="true"/>
    <script type="text/javascript">
        var grid = null;
        var addGSData_s = {};

        $(function () {
            $("#searchBar").ligerForm();
            $("#ligergrid1").ligerGrid({
                url: '<%=path%>/custpay/cmmRechargeRecord/initCmmRechargeRecordList.do',
                method : 'get',
                parms:getSearchParams(),
                columns: [
                    { display: '订单号', name: 'id', width: 150, align: 'center', enabledSort:true},
                    { display: '业务类型', name: 'busType', width: 150, align: 'center', enabledSort:false,
                        render:function(rowdata,index,value){
                            switch (rowdata.busType) {
                                case '0': return '分期还款';
                                case '1': return '经营贷结清';
                                case '2': return '专款专用';
                                case '3': return '保险业务';
                                case '4': return '保险业务';
                                case '5': return '保险业务';
                                case '6': return '员工内部理财';
                                case '7': return '法人还款';
                                case '8': return '挂靠公司还款';
                                case '9': return '自然人还款';
                                case '10': return '融资租赁结清';
                                case '11': return '宜宾资管';
                                case '12': return '再融资';
                                case '13': return '机构回款';
                                case '14': return '疫情贷';
                                case '15': return '融资租赁分期';
                                case '16': return '保证金担保';
                                case '17': return '代偿担保';
                                default: return '';
                            }
                        }
                    },
                    { display: '还款手机', name: 'clientPhone', width: 100, align: 'center', enabledSort:true},
                    { display: '还款客户', name: 'clientName', width: 100, align: 'center', enabledSort:true},
                    { display: '支付状态', name: 'isSucNm', width: 80, align: 'center', enabledSort:true},
                    { display: '结果描述', name: 'resultDesc', width: 80, align: 'center', enabledSort:true},
                    { display: '还款金额(元)', name: 'paymentMoneydou', width: 100, align: 'center', enabledSort:true},
                    { display: '支付类型', name: 'paymentTypeNm', width: 100, align: 'center', enabledSort:true},
                    { display: '是否自动代扣', name: 'isAutoChargeNm', width: 100, align: 'center', enabledSort:true},
                    { display: '还款时间', name: 'crtTm', width: 150, render:renderDate, align: 'center', enabledSort:true},
                    { display: '处理状态', name: 'prpslStsNm', width: 100, align: 'center', enabledSort:true},
                    { display: '处理结果', name: 'finResult', width: 150, align: 'center', enabledSort:true},
                    { display: '处理人', name: 'mdfUsrNm', width: 100, align: 'center', enabledSort:true},
                    {
                        display: '操作',width: 300,align: 'center',
                        render: function (rowdata, rowindex, value){
                            console.log(rowdata);
                            var h3 = '';
                            if(rowdata.prpslSts!='2'){
                                h3 = '<a class="sq-button-hasicon" href="javascript:void(0);" onclick="modifyPrpslSts('+rowindex+')"><i class="sqBtnIco_4"></i>处理</a>'
                            }
                            var h4 = '';
                            if(rowdata.paymentType != 1) {
                                h4 = '<a class="sq-button-hasicon" href="javascript:void(0);" onclick="tongbu(' + rowdata.id + ')"><i class="sqBtnIco_4"></i>同步</a>'
                            }
                            if(rowdata.paymentType ==1 && rowdata.payType == 4 ){
                                //企业还款同步
                                h4 = '<a class="sq-button-hasicon" href="javascript:void(0);" onclick="enterTongbu(' + rowdata.id + ')"><i class="sqBtnIco_4"></i>同步</a>'
                            }

                            var h5 = '';
                            if(rowdata.isSuc == 1){
                                h5 = '<a class="sq-button-hasicon" href="javascript:void(0);" onclick="fundDecompose('+rowdata.id+')"><i class="sqBtnIco_4"></i>订单分解</a>'
                            }
                            var h6 = '';
                            h6 = '<a class="sq-button-hasicon" href="javascript:void(0);" onclick="queryChl('+rowdata.id+')"><i class="sqBtnIco_4"></i>渠道查询</a>'

                            var end = h3 + h4 + h5 + h6;
                            return end;
                        }
                    }
                ],
                pageSize:10,
                root:'rows',
                record:'total',
                hideLoadButton : true,
                width: '100%',
                height:'100%',
                heightDiff:-2,
                //checkbox:true,
                rownumbers:false,
                onBeforeShowData: function ()
                {
                    var grid = this;
                    grid.collapsedRows = [];
                },
                detail: {
                    height: 'auto',
                    onShowDetail: showDetail_grid
                }
            });
            grid = liger.get("ligergrid1");

            $("#searchBtn").click(function(event) {
                var form = new liger.get("searchBar");
                var data = form.getData();
                console.log(data);
                for (name in data){
                    grid.setParm(name,data[name]);
                };
                grid.reload();
            });

            //同步保存
            $("#saveTongbuRepay_Btn").click(function (event) {
                var form = liger.get("tongbuRepay_form");
                if (!form.valid()) {
                    $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
                    return false; //验证不通过，不执行以下代码
                }
                var data = form.getData();
                //默认启用
                data.isValid = "1";
                //$.ligerDialog.hide();
                //以下应该有一段Ajax交互
                var wait_dialog;

                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    data: data,
                    url: '<%=path%>/custpay/cmmOrderQuery/modify.do?id='+addGSData_s.id,
                    beforeSend: function () {
                        wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                    },
                    success: function (msg) {
                        wait_dialog.close();
                        if (msg.success == 1) {
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                            setTimeout(function(){
                                $('.l-dialog-win').hide();
                                $('.l-window-mask').hide();
                            },1000);
                            bankHeadQ = null;
                            grid.reload($('.pcontrol input', grid.toolbar).val());
                        } else {
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                        }
                    }
                });
            });


            $("#saveChl_Btn").click(function(event) {
                setTimeout(function(){
                    $('.l-dialog-win').hide();
                    $('.l-window-mask').hide();
                },10);
            });

            $("#clearBtn").click(function(event) {
                clearLigerForm("searchBar");
            });
            $("#dateform").ligerForm();
        });

        function showDetail_grid(row, detailPanel, callback){
            var gridBox = document.createElement('div');
            $(gridBox).css('margin', 10);
            $(detailPanel).css({
                'padding': 10,
                'box-sizing': 'border-box'
            }).append(gridBox);
            detailGrid = $(gridBox).ligerGrid({
                url: '<%=path%>/custpay/cmmOlRecordDetail/initCmmOlRecordDetailList.do?recordId='+row.id,
                method: 'get',
                columns: [
                    {display: '支付表', name: 'paylistCode', width: 200, align: 'center'},
                    {display: '设备名称', name: 'equipmentName', width: 150, align: 'center'},
                    {display: '租期总数', name: 'termTotal', width: 70, align: 'center'},
                    {display: '当前期数', name: 'termCurrent', width: 70, align: 'center'},
                    {display: '租金(元)', name: 'rentMoney', width: 90, align: 'center', enabledSort: false,
                        render:function(rowdata,index,value){
                            return '&yen;'+FormatMoney(rowdata.rentMoney/100);
                        }
                    },
                    {display: '罚息(元)', name: 'fineMoney', width: 90, align: 'center', enabledSort: false,
                        render:function(rowdata,index,value){
                            return '&yen;'+FormatMoney(rowdata.fineMoney/100);
                        }
                    },
                    {display: '实际付款金额(元)', name: 'paymentMoney', width: 90, align: 'center',
                        render:function(rowdata,index,value){
                            return '&yen;'+FormatMoney(rowdata.paymentMoney/100);
                        }
                    },
                    {display: '分解状态', name: 'isDecSucNm', width: 100, align: 'center'},
                    {display: '资金编号', name: 'fundCode', width: 200, align: 'center'}
                ],
                root: 'rows',
                hideLoadButton : true,
                width: '100%',
                height: 'auto',
                isScroll: false,
                rownumbers:true,  //显示序号
                checkbox:true,
                allowUnSelectRow:true,
                alternatingRow: true,
                onAfterShowData: callback,
                frozen: false,
                usePager: false
            });
        }

        //查询参数
        function getSearchParams(){
            var form = new liger.get("searchBar");
            var data = form.getData();
            return data;
        }
        //格式化时间
        function renderDate(rowdata, index, value){
            var dateStr = "";
            if(null != value){
                var date = new Date(value);
                dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
            }
            return dateStr;
        }
        //导出

        function exportInfBfsPayExcel(){
            var form = liger.get("searchBar");
            var data = form.getData();
            var req="format=xls";
            for (name in data) {
                for (name in data) {
                    if(data[name]){
                        req+="&"+name+"="+data[name];
                    }
                }
            }
            window.location.href="<%=path%>/custpay/cmmRechargeRecord/exportCmmRechargeRecordExcel.do?"+req;
        }

        //查询支付成功分解失败订单并分解
        function autoFundDecompose(){
            var _url = "<%=path%>/custpay/cmmRechargeRecord/autoFundDecompose.do";
            var _params = {};
            AjaxUtils.ajaxPostWithSQconfirm(_url, _params, function(data){
                if(data.success == 1){
                    $.ligerDialog.SQalert({content: data.info, SQ_type: "success"});
                }else{
                    UIUtils.alertError(data.info);
                }
            }, true, "是否进行查询成功分解失败订单分解?");
        }
        //处理操作
        function modifyPrpslSts(rowindex){
            var rowdata = grid.data.rows[rowindex];
            console.log(rowdata);
            var _id = rowdata.id;
            if(_id == ''){
                top.$.ligerDialog.SQalert({content: "请重新操作",SQ_type: "warning"});
                return false; //验证不通过，不执行以下代码
            }
            var _prpslSts = rowdata.prpslSts;
            var _finResult = rowdata.finResult;
            if(_prpslSts !== ''){
                liger.get("modifyprpslSts").setValue(_prpslSts);
            }
            if(_finResult !== ''){
                liger.get("modifyfinResult").setValue(_finResult);
            }
            var _dialog = $.ligerDialog.open({
                target: $("#modifyPrpslStsdiv"),
                title: '处理',
                width: 900,
                height: 120,
                isResize: true,
                modal: true,
                buttons: [{ text: '确定', onclick: function (i, d) {
                    var modifyprpslSts = liger.get("modifyprpslSts").getValue();
                    if (modifyprpslSts == '' || modifyprpslSts == ' ') {
                        top.$.ligerDialog.SQalert({content: "请选择处理状态",SQ_type: "warning"});
                        return false; //验证不通过，不执行以下代码
                    }
                    var modifyfinResult = liger.get("modifyfinResult").getValue();
                    liger.get("modifyprpslSts").setValue(" ");
                    liger.get("modifyfinResult").setValue("");
                    _dialog.hide();
                    var wait_dialog;
                    $.ajax({
                        type: 'post',
                        dataType:'json',
                        data:'id='+_id+'&prpslSts='+modifyprpslSts+'&finResult='+modifyfinResult,
                        url: '<%=path%>/custpay/cmmRechargeRecord/modify.do',
                        beforeSend: function(){
                            wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
                        },
                        success:function(msg){
                            wait_dialog.close();
                            if(msg.success == 1){
                                $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
                                grid.reload();
                            }else{
                                $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
                            }
                        }
                    });

                }
                }, { text: '关闭', onclick: function (i, d) {
                    liger.get("modifyprpslSts").setValue(" ");
                    liger.get("modifyfinResult").setValue("");
                    _dialog.hide();
                }
                }]
            });
        }
        /**
        //导出
        function exportInfBfsPayExcel(){
            var url = '<%=path%>/custpay/cmmRechargeRecord/exportCmmRechargeRecordExcel.do';
            $("#searchBar").attr('action',url);
            $("#searchBar").submit();
            $("#searchBar").attr('action',"");
        }
         **/

        //同步
        function tongbu(_id) {
            $.ajax({
                type: 'get',
                dataType: 'json',
                url: '<%=path%>/custpay/cmmOrderQuery/getByOrderId.do?orderid=' + _id,
                success: function (msg) {
                    if (msg.success == 1) {
                        //$.ligerDialog.success(msg.info)
                        openTongbu();//弹出修改框
                        setData(msg.data);//设置数据
                    } else {
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                    }
                }
            });
        }

        function openTongbu() {
            $.ligerDialog.open({
                width: 400,
                top: 100,
                title: '同步订单信息',
                target: $("#tongbuRepay_Html"),
                onHideDialog: function () {
                    clearLigerForm("tongbuPay_Html");
                }
            });

            $("#tongbuRepay_form").ligerForm({validate: true});
        }

        //设置页面数据
        function setData(data) {
            var form = new liger.get("tongbuRepay_form");
            console.log(data)
            addGSData_s.localSeqId = data.localSeqId;
            addGSData_s.id = data.id;
            console.log(data)
            var ResultDesc = '';
            switch(data.isSuc){
                case 0:
                    ResultDesc = '失败';
                    break;
                case 1:
                    ResultDesc = '成功';
                    break;
                case 2:
                    ResultDesc = '未支付';
                    break;
                case 3:
                    ResultDesc = '处理中';
                    break;
            }
                form.setData({
                daikouResult: ResultDesc,
                daikouResultDesc: data.resultDesc,
                payAmt: data.paymentMoney/100,
                daikouTime: DateUtils.formatDate(new Date(data.crtTm), DateUtils.YYYY_MM_DDHHMMSS),
                payTypes: data.payTypeNm
            });
            //设置选中

        }


        //同步
        function queryChl(_id) {
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: '<%=path%>/custpay/cmmRechargeRecord/getChlInfo.do?orderid=' + _id,
                success: function (msg) {
                    if (msg.success == 1) {
                        //$.ligerDialog.success(msg.info)
                        openChl();//弹出修改框
                        setDataChl(msg.data);//设置数据
                    } else {
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                    }
                }
            });
        }

        function openChl() {
            $.ligerDialog.open({
                width: 400,
                top: 100,
                title: '渠道信息',
                target: $("#openChl_Html"),
                onHideDialog: function () {
                    clearLigerForm("openChl_Html");
                }
            });

            $("#openChl_form").ligerForm({validate: true});
        }

        //设置页面数据
        function setDataChl(data) {
            console.log("channel is:"+data)
            var form = new liger.get("openChl_form");
            form.setData({
                channelNm: data
            });
            //设置选中
        }


        //订单分解
        function fundDecompose(_id) {
            if (!_id)return;
            $.ligerDialog.confirm('确定分解吗?', function (flag) {
                if (flag == false)return;
                var wait_dialog;
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    url: '<%=path%>/custpay/cmmRechargeRecord/fundDecompose.do?id=' + _id,
                    beforeSend: function () {
                        wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                    },
                    success: function (msg) {
                        wait_dialog.close();
                        if (msg.success == 1) {
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                            grid.reload($('.pcontrol input', grid.toolbar).val());
                        } else {
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                        }
                    }
                });
            });
        }

        //企业订单同步
        function enterTongbu(_id) {
            if (!_id)return;
            $.ligerDialog.confirm('确定同步吗?', function (flag) {
                if (flag == false)return;
                var wait_dialog;
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    url: '<%=path%>/custpay/cmmOrderQuery/paautodecoposeFund.do?id='+_id,
                    beforeSend: function () {
                        wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                    },
                    success: function (msg) {
                        wait_dialog.close();
                        if (msg.success == 1) {
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                            grid.reload($('.pcontrol input', grid.toolbar).val());
                        } else {
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                        }
                    }
                });
            });
        }

    </script>
</head>

<body class="iframeBody">
<div class="Wrapper">
    <div class="searchArea">
        <form id="searchBar">
            <div class="fieldset1 clearFix">
                <div class="fieldBox">
                    <div class="label">状态</div>
                    <div class="field">
                        <select name="isSuc"  id="isSuc"  disabled="disabled">
                            <option value="  " selected="selected">--请选择--</option>
                            <c:forEach items="${statuslist}" var="opt">
                                <option value="${opt.key}">${opt.value}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">订单号</div>
                    <div class="field">
                        <input name="orderid" id="orderid" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">手机号</div>
                    <div class="field">
                        <input name="clientPhone" id="clientPhone" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">姓名</div>
                    <div class="field">
                        <input name="clientName" id="clientName" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">流水号</div>
                    <div class="field">
                        <input name="platformSeqId" id="platformSeqId" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <div class="fieldBox col_33">
                    <div class="label">还款时间(起)</div>
                    <div class="field">
                        <input name="crtTmBegin" id="crtTmBegin" type="text" class="ui-datepicker" value="">
                    </div>
                </div>
                <div class="fieldBox col_33">
                    <div class="label">还款时间(止)</div>
                    <div class="field">
                        <input name="crtTmEnd" id="crtTmEnd" type="text" class="ui-datepicker" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">是否自动代扣</div>
                    <div class="field">
                        <select name="isAutoCharge"  id="isAutoCharge"  disabled="disabled">
                            <option value="  " selected="selected">--请选择--</option>
                            <option value="1">是</option>
                            <option value="0">否</option>
                        </select>
                    </div>
                </div>
                <div class="fieldBox col_33">
                    <div class="label">处理状态</div>
                    <div class="field">
                        <select name="prpslSts" id="prpslSts" validate="{required:true,select:true}">
                            <option value=" " >请选择</option>
                            <option value="0" >未处理</option>
                            <option value="1" >处理中</option>
                            <option value="2" >已处理</option>
                        </select>
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">身份证号</div>
                    <div class="field">
                        <input name="idCard" id="idCard" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <%--<div class="fieldBox col_33">
                    <div class="label">支付平台</div>
                    <div class="field">
                        <select name="payType"  id="payType"  disabled="disabled">
                            <option value=" " selected="selected">--请选择--</option>
                            <c:forEach items="${payTypList}" var="opt">
                                <option value="${opt.key}">${opt.value}</option>
                            </c:forEach>
                        </select>
                    </div>
                    <div style="display: none">
                    </div>
                </div>--%>
                <div class="clearFix"></div>
                <div class="btnsBox">
                    <span id="searchBtn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_1"></i>查询</span>
                    <span id="clearBtn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_19"></i>清空</span>
                    <!--<span class="sq-button sq-button-hasicon" onclick="exportInfBfsPayExcel()"><i class="sqBtnIco_21"></i>导出</span>-->
                    <%--<span class="sq-button sq-button-hasicon" onclick="autoFundDecompose()"><i class="sqBtnIco_46"></i>批量自动分解</span>--%>
                    <input id="resetBtn" type="reset" value="重置" class="l-button l-button-reset Hide"/>
                </div>
            </div>
        </form>
    </div>
</div>
<div id="ligergrid1" class="sq-grid"></div>
<div id="modifyPrpslStsdiv" style="display:none">
    <form id="dateform">
        <div class="fieldset1 clearFix">
            <div class="fieldBox col_100_auto">
                <div class="label">处理状态</div>
                <div class="field">
                    <select name="modifyprpslSts" id="modifyprpslSts" validate="{required:true,select:true}">
                        <option value=" " >请选择</option>
                        <option value="0">未处理</option>
                        <option value="1">处理中</option>
                        <option value="2">已处理</option>
                    </select>
                </div>
            </div>
            <div class="clearFix"></div>
            <div class="fieldBox col_100_auto">
                <div class="label">处理结果</div>
                <div class="field">
                    <input name="modifyfinResult" id="modifyfinResult" type="text" ligerui="width:700" class="ui-textbox" value="">
                </div>
            </div>
        </div>
    </form>
</div>

<!-- 同步信息弹出框 -->
<div id="tongbuRepay_Html" style="display:none">
    <form id="tongbuRepay_form">
        <div class="fieldset1 tabel_col tabel_col_1 clearFix">

            <div class="fieldBox col_100">
                <div class="label">代扣结果</div>
                <div class="field">
                    <input name="daikouResult" id="daikouResult" type="text" class="ui-textbox" value=""  readonly="readonly">
                </div>
            </div>


            <div class="fieldBox col_100">
                <div class="label">结果描述</div>
                <div class="field">
                    <input name="daikouResultDesc" id="daikouResultDesc" type="text" class="ui-textbox" value=""  readonly="readonly">
                </div>
            </div>
            <div class="fieldBox col_100">
                <div class="label">扣款金额(元)</div>
                <div class="field">
                    <input name="payAmt" id="payAmt" type="text" class="ui-textbox" value=""  readonly="readonly">
                </div>
            </div>
            <div class="fieldBox col_100">
                <div class="label">扣款时间</div>
                <div class="field">
                    <input name="daikouTime" id="daikouTime" type="text" class="ui-textbox" value=""  readonly="readonly">
                </div>
            </div>
            <div class="fieldBox col_100">
                <div class="label">支付平台</div>
                <div class="field">
                    <input name="payTypes" id="payTypes" type="text" class="ui-textbox" value=""  readonly="readonly">
                </div>
            </div>

        </div>
        <div class="btnsBox">
            <span id="saveTongbuRepay_Btn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_10"></i>保存</span>
        </div>
    </form>
</div>


<!-- 同步信息弹出框 -->
<div id="openChl_Html" style="display:none">
    <form id="openChl_form">
        <div class="fieldset1 tabel_col tabel_col_1 clearFix">

            <div class="fieldBox col_100">
                <div class="label">代扣渠道</div>
                <div class="field">
                    <input name="channelNm" id="channelNm" type="text" class="ui-textbox" value=""  readonly="readonly">
                </div>
            </div>
        </div>
        <div class="btnsBox">
            <span id="saveChl_Btn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_10"></i>确定</span>
        </div>
    </form>
</div>




</body>
</html>
